package com.HooDee.netty.secondeDemo;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * Package: com.HooDee.netty.secondeDemo
 * Description：
 * Author: 蒋豪
 * Date:  2021.09.10 22:18
 * Modified By:
 */
public class MyClientHandler extends SimpleChannelInboundHandler<String> {

    /**
     * 客户端接收到消息的时候进入此方法
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(ctx.channel().remoteAddress()+ ", " + msg);
        // 接受服务端发送消息
        ctx.writeAndFlush("来之客户端发送的消息" + LocalDateTime.now());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush("客户端连接");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
